package orange.com.openstack.sahara.api;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import net.minidev.json.JSONObject;

import com.google.gson.Gson;
import com.jayway.jsonpath.JsonPath;

import orange.com.openstack.sahara.data.DataSource;
import orange.com.openstack.sahara.httpConnection.RestCall;

/**
 * class allowing the interaction with the datasource Rest service
 * @author osvaldo.cocucci@gmail.com
 * 
 */
public class DataSources {

	String endpoint;
	String tenantId;
	String token;
	Gson gson;

	/**
	 * @param endpoint
	 * @param tenantId
	 * @param token
	 * 
	 *            constructor
	 */
	public DataSources(String endpoint, String tenantId, String token) {
		gson = new Gson();
		this.endpoint = endpoint;
		this.tenantId = tenantId;
		this.token = token;
	}

	/**
	 * 
	 * list all data sources according to provided parameters
	 */
	public List<DataSource> listDataSource() {
		List<DataSource> dataSources = new ArrayList<DataSource>();
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/data-sources", null, "GET", token);
		try {
			if (connection.getResponseCode() != 200)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();
			List<JSONObject> dataSourcesJSON = JsonPath.read(JSONreponse,
					"$.data_sources[*]");

			for (JSONObject json : dataSourcesJSON) {
				dataSources.add(gson.fromJson(json.toJSONString(),
						DataSource.class));
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dataSources;
	}

	/**
	 * @param dataSourceId
	 * 
	 *            show the data source detail according to the id
	 */
	public DataSource showDataSource(String dataSourceId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/data-sources/" + dataSourceId, null, "GET", token);
		try {
			if (connection.getResponseCode() != 200)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();

			return gson.fromJson(((JSONObject) JsonPath.read(JSONreponse,
					"$.data_source")).toJSONString(), DataSource.class);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param desc
	 * @param url
	 *            e.g "swift://container.sahara/text"
	 * @param password
	 * @param user
	 * @param type
	 *            can be "swift"
	 * @param name
	 * 
	 *            create a datasource for sahara
	 */
	public DataSource createDataSource(String desc, String url,
			String password, String user, String type, String name) {
		RestCall caller = new RestCall();
		String payload = "{\"description\": \"" + desc + "\",\"url\": \"" + url
				+ "\",\"credentials\": {\"password\": \"" + password
				+ "\",\"user\": \"" + user + "\"},\"type\": \"" + type
				+ "\",\"name\": \"" + name + "\"}";
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/data-sources", payload, "POST", token);
		try {
			if (connection.getResponseCode() != 202)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();
			return gson.fromJson(((JSONObject) JsonPath.read(JSONreponse,
					"$.data_source")).toJSONString(), DataSource.class);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param dataSourceId
	 * 
	 *            delete the data source according to the id
	 */
	public boolean deleteDataSource(String dataSourceId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/data-sources/" + dataSourceId, null, "DELETE", token);
		try {
			if (connection.getResponseCode() == 204)
				return true;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}
